<?php


/*  ----------------------------------------------------------------------------
    background support
 */


add_theme_support('custom-background', array(
    'default-color' => '',
    'default-image' => ''
));



class td_background {

    var $is_boxed_layout = false; //if it's a the box version of the theme (we have bg color or a bg)
    var $is_stretched_bg = false; //if it's a stretched background


    var $theme_bg_color = ''; //the background color of the theme

    var $category_background = ''; //use this bg from category and posts if not empty
    var $category_bg_style = '';




    function __construct() {
        add_action('wp_head', array($this, 'wp_head_hook'), 10);

        //clean up the wp custom-background class if needed
        add_filter('body_class', array($this,'add_slug_to_body_class'));
    }




    //we have to hook our filters to wp_head action - for them to work in theme customizer
    function wp_head_hook() {
        if ((get_background_color() != '' and get_background_color() != 'ffffff' and get_background_color() != '#ffffff') or get_background_image() != '') {
            $this->is_boxed_layout = true;
        }

        if (td_util::get_customizer_option('stretch_background') == 'yes') {
            $this->is_stretched_bg = true;
        }

        $this->theme_bg_color  = get_background_color();



        $use_settings_from_category_id = ''; //if not empty, the theme will load the settings from this category id

        //read the category background
        if (is_category()) {
            $use_settings_from_category_id = get_query_var('cat');
        }

        if (is_single()) {
            $use_settings_from_category_id = td_global::get_primary_category_id();
        }



        //echo $use_settings_from_category_id;
        if ($use_settings_from_category_id != '') {
            //get the bg
            $tdc_image = get_tax_meta($use_settings_from_category_id, 'tdc_image');
            if (!empty($tdc_image['src'])) {
                $this->is_boxed_layout = true;
                $this->category_background = $tdc_image['src'];
            }

            //get the bg color
            $tdc_bg_color = get_tax_meta($use_settings_from_category_id, 'tdc_bg_color');
            if (!empty($tdc_bg_color) and $tdc_bg_color != '#') {
                $this->theme_bg_color = str_replace('#', '', $tdc_bg_color);
            }

            //get the bg style
            $tdc_bg_repeat = get_tax_meta($use_settings_from_category_id, 'tdc_bg_repeat');
            if ($tdc_bg_repeat == '') {
                $this->is_stretched_bg = true;
            } else {
                $this->is_stretched_bg = false;
            }
        }



        if ($this->is_stretched_bg == true) {
            //add the js filter for our custom bg
            add_filter( 'td_js_buffer_render', array($this, 'add_js_hook'));

            //add custom color css
            add_filter( 'td_css_buffer_render', array($this, 'add_css_color_hook'));
        } else {
            //add custom bg function css - this is added from the category
            add_filter( 'td_css_buffer_render', array($this, 'add_css_custom_background'));
        }


    }

    //custom background js
    function add_js_hook($js) {

        if (!empty($this->category_background)) {
            $td_background = $this->category_background;
        } else {
            $td_background = get_background_image();
        }



        if (!empty($td_background) and $this->is_stretched_bg == true) {
            $js .= "\n" . "
            //custom backstretch background
            jQuery().ready(function() {
                jQuery.backstretch('$td_background', {fade:1200});
            });
             ";
        }

        return $js;
    }


    //custom background css hook - for color used by the stretched background
    function add_css_color_hook($css) {
        if (!empty($this->theme_bg_color)) {
            $css.= "\n" . '
                body {
                    background-color:#' . $this->theme_bg_color . ';
                }
            ';
        }
        return $css;
    }

    //called with a category custom bg (emulates the wp function)
    function add_css_custom_background($css) {
        //this is the custom css generated by the theme when a category bg is enabled
        if (!empty($this->theme_bg_color)) {
            $css.= "\n" . '
                body {
                    background-color:#' . $this->theme_bg_color . ';
                }
            ';
        }

        if (!empty($this->category_background)) {
            $css.= "\n" . "
                body {
                    background-image:url('" . $this->category_background . "');
                    background-repeat:repeat;
                }
            ";
        }
        return $css;
    }


    function add_slug_to_body_class($classes) {
        //custom stretching background
        if ($this->is_stretched_bg or $this->category_background != '') {
            //remove the default word press class if it's stretched or if it's coming from a category bg
            $i = 0;
            foreach ($classes as $key => $value) {
                if ($value == 'custom-background') {
                    unset($classes[$i]);
                }
                $i++;
            }
        }

        if ($this->is_boxed_layout) {
            $classes[] = 'td-boxed-layout';
        } else {
            $classes[] = 'td-full-layout';
        }

        return $classes;
    }


}

new td_background();


